package com.justgame.project.cloud.common.util;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.google.common.base.CaseFormat;
import org.springframework.lang.NonNull;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * MyBatis通用工具类
 */
public class MyBatisUtils {

    public static String buildSelectAlias(String tableAliasName,String tableColumnName,String aliasObjectName,String aliasFieldName){
        if (StringUtils.hasLength(aliasObjectName)){
            final String AliasFormat = "%s.%s AS `%s.%s`";
            return String.format(AliasFormat,tableAliasName,tableColumnName,aliasObjectName,aliasFieldName);
        }
        return buildSelectAlias(tableAliasName,tableColumnName,aliasFieldName);
    }

    public static String buildSelectAlias(String tableAliasName,String tableColumnName,String aliasFieldName){
        final String AliasFormat = "%s.%s AS `%s`";
        return String.format(AliasFormat,tableAliasName,tableColumnName,aliasFieldName);
    }



    /**
     * 通过@TableName注解获取实体类对应表名
     * @param clazz 实体类
     * @return 表名
     */
    public static String getAnnotationTableName(Class<?> clazz){
        if (null == clazz){
            return StringPool.EMPTY;
        }
        return Optional.ofNullable(clazz.getAnnotation(TableName.class))
                .map(TableName::value)
                .orElse(getAnnotationTableName(clazz.getSuperclass()));
    }

    /**
     * 通过表明获取实体名
     * @param tableName 表名
     * @return 实体名
     */
    public static String firstUpperTableEntityName(String tableName){
        return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL,tableName);
    }

    /**
     * 通过表明获取实体名
     * @param tableName 表名
     * @return 实体名
     */
    public static String firstLowerTableEntityName(String tableName){
        return firstWorldLower(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL,tableName));
    }

    /**
     * 通过表明获取实体名
     * @return 实体名
     */
    public static String firstLowerTableEntityName(Class<?> clazz){
        String tableName = getAnnotationTableName(clazz);
        if (StringUtils.isEmpty(tableName)) {
            return StringPool.EMPTY;
        }
        return firstLowerTableEntityName(tableName);
    }

    /**
     * 首字母大写
     * @param str
     * @return
     */
    public static String firstWorldUpper(@NonNull String str){
        if (str.length() > 1){
            return str.substring(0,1).toUpperCase() + str.substring(1);
        }
        return str.toUpperCase();
    }

    /**
     * 首字母大写
     * @param str
     * @return
     */
    public static String firstWorldLower(@NonNull String str){
        if (str.length() > 1){
            return str.substring(0,1).toLowerCase() + str.substring(1);
        }
        return str.toUpperCase();
    }

    public static List<String> tableClassFieldNames(Class<?> clazz){
        List<String> fieldNames = Arrays.stream(clazz.getDeclaredFields()).map(Field::getName).collect(Collectors.toList());
        if (null != clazz.getSuperclass()){
            fieldNames.addAll(tableClassFieldNames(clazz.getSuperclass()));
        }
        return fieldNames;
    }
}
